package com.rujian.passwordmanager.ui;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.rujian.passwordmanager.R;
import com.rujian.passwordmanager.bean.PasswordBean;
import com.rujian.passwordmanager.db.dao.PwdDao;
import com.rujian.passwordmanager.util.KeyboardUtil;
import com.rujian.passwordmanager.view.MyEditText;

public class PwdDetailActivity extends AppCompatActivity {
    private PasswordBean passwordBean = null;
    private EditText edt_keyword,edt_account,edt_nicname,edt_tip;
    private Button btn_OK,btn_chg_cancel;
    private PwdDao pwdDao;
    private MyEditText myedit;
    private LinearLayout ll_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd_detail);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);//不随屏幕旋转
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//编辑状态
                setEditable(true);
            }
        });

        edt_keyword = (EditText)findViewById(R.id.edt_item_key);
        edt_account = (EditText)findViewById(R.id.edt_item_account);
        edt_nicname = (EditText)findViewById(R.id.edt_item_nicname);
        edt_tip = (EditText)findViewById(R.id.edt_item_tip);
        btn_OK = (Button)findViewById(R.id.btn_OK);
        myedit = (MyEditText)findViewById(R.id.myedit);
        btn_chg_cancel = (Button)findViewById(R.id.btn_chg_cancel);
        ll_button = (LinearLayout)findViewById(R.id.ll_button);

        passwordBean = (PasswordBean)getIntent().getSerializableExtra("pwdDetail");
        if(passwordBean != null){
            edt_keyword.setText(passwordBean.mg_keyWord);
            edt_account.setText(passwordBean.mg_account);
            edt_nicname.setText(passwordBean.mg_nicname);
            myedit.setText(passwordBean.mg_password);
            edt_tip.setText(passwordBean.mg_tip);
            toolbar.setSubtitle(passwordBean.mg_nicname);
        }
        setSupportActionBar(toolbar);
        btn_OK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String key = edt_keyword.getText().toString();
                String account = edt_account.getText().toString();
                String nicname = edt_nicname.getText().toString();
                String password = myedit.getText().toString();
                String tip = edt_tip.getText().toString();

                if(key == null || key.equals("")){
                    Toast.makeText(PwdDetailActivity.this, "关键字是搜索时必须的", Toast.LENGTH_SHORT).show();
                }else if(account == null || account.equals("")){
                    Toast.makeText(PwdDetailActivity.this,"账号为空",Toast.LENGTH_SHORT).show();
                }else if(password == null || password.equals("")){
                    Toast.makeText(PwdDetailActivity.this,"密码为空",Toast.LENGTH_SHORT).show();
                }else{
                    pwdDao = new PwdDao(PwdDetailActivity.this);
                    PasswordBean pb = new PasswordBean();
                    pb.mg_id = passwordBean.mg_id;
                    pb.mg_keyWord = key;
                    pb.mg_account = account;
                    pb.mg_nicname = nicname;
                    pb.mg_password = password;
                    pb.mg_tip = tip;
                    switch(pwdDao.chgAccount(pb)){
                        case 0x124:
                            Toast.makeText(PwdDetailActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                            passwordBean.mg_keyWord = pb.mg_keyWord;
                            passwordBean.mg_account = pb.mg_account;
                            passwordBean.mg_nicname = pb.mg_nicname;
                            passwordBean.mg_password = pb.mg_password;
                            passwordBean.mg_tip = pb.mg_tip;
                            setEditable(false);
                            break;
                        case 0x125:
                            Toast.makeText(PwdDetailActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                            break;
                        case 0x126:
                            Intent intent = new Intent(PwdDetailActivity.this, LoginActivity.class);
                            startActivity(intent);
                            break;
                    }
                }
            }
        });
        btn_chg_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_keyword.setText(passwordBean.mg_keyWord);
                edt_account.setText(passwordBean.mg_account);
                edt_nicname.setText(passwordBean.mg_nicname);
                myedit.setText(passwordBean.mg_password);
                edt_tip.setText(passwordBean.mg_tip);
                setEditable(false);
            }
        });

//        myedit.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD);//设置输入法为空
//        myedit.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                new KeyboardUtil(PwdDetailActivity.this, PwdDetailActivity.this, myedit).showKeyboard();
//                return false;
//            }
//        });
    }

    private void setEditable(boolean is){
        edt_keyword.setEnabled(is);
        edt_account.setEnabled(is);
        edt_nicname.setEnabled(is);
        myedit.setEnabled(is);

        edt_tip.setEnabled(is);
        if(is){
            ll_button.setVisibility(View.VISIBLE);
        }else{
            ll_button.setVisibility(View.GONE);
        }
    }

}
